﻿<%@ WebHandler Language="C#" Class="ZSUnqualifiedTrialReport" %>

using System;
using System.IO;
using System.Web;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Converters;
using System.Collections.Generic;
using System.Data;
using ReportBusiness;

/**
 * Description: 装配试车厂不合格品审理报表
 * Copyright (c) : 通力凯顿（北京）系统集成有限公司
 * Writer:zhunan
 * Date:2022.11.25
 **/

public class ZSUnqualifiedTrialReport : IHttpHandler
{
    uMESReportOracleDAL dal = new uMESReportOracleDAL();
    public bool IsReusable { get { return false; } }
    public void ProcessRequest(HttpContext context)
    {
        string requestjson = "";
        using (StreamReader reader = new StreamReader(context.Request.InputStream)) { requestjson = reader.ReadToEnd(); }
        JObject jObject = JsonConvert.DeserializeObject(requestjson) as JObject;
        if (jObject["type"] != null)
        {
            try
            {
                Result r = new Result();
                context.Response.ContentType = "text/json";
                string type = jObject["type"].ToString();
                switch (type)
                {
                    case "getData":
                        r = getData(jObject);//查询
                        context.Response.Write(JsonConvert.SerializeObject(r));
                        break;
                    case "exportExcel":
                        exportExcel(jObject);//导出
                        break;
                    default:
                        break;
                }
            }
            catch (Exception e)
            {
                Result r = new Result();
                r.data = null;
                r.meta = new Result.Meta(500, e.Message);
                r.total = 0;
                context.Response.ContentType = "text/json";
                context.Response.Write(JsonConvert.SerializeObject(r));
            }
        }
    }

    string getQuerySql(JObject jObject)
    {
        string sql = @"                   
SELECT u.attributevalue,cn.zjx2,cn.matnr,p.description,cn.zbhgps,e.fullname checkemployee,
        cn.createdate,cn.zbhgdh,cn.zbhglb,cn.zqxtxt,cn.zsljl,cn.checkdate,cn.zzttxt
FROM ContainerNonconformingInfo cn
LEFT JOIN productbase pb ON cn.matnr=pb.productname
LEFT JOIN product p ON pb.revofrcdid=p.productid
LEFT JOIN enginepartplan ep ON cn.containerid=NVL(ep.containerid,ep.enginecontainerid)
LEFT JOIN workflowstep wfs ON wfs.workflowstepid=ep.workflowstepid
LEFT JOIN mfgorder m ON m.mfgordername=cn.fertaufnr
LEFT JOIN Userattribute u ON m.mfgorderid=u.parentid AND u.userattributename='发动机'
LEFT JOIN employee e ON e.employeeid = cn.checkemployeeid
WHERE cn.isuse='0' AND cn.zsldw = '603'";

        //发动机号
        if (jObject["engineno"] != null && jObject["engineno"].ToString() != "")
        {
            sql += string.Format(" AND u.attributevalue = '{0}'", jObject["engineno"].ToString());
        }

        //审理单号
        if (jObject["zbhgdh"] != null && jObject["zbhgdh"].ToString() != "")
        {
            sql += string.Format(" AND cn.zbhgdh LIKE '%{0}%'", jObject["zbhgdh"].ToString());
        }

        //审理类型
        if (jObject["zbhglb"] != null && jObject["zbhglb"].ToString() != "")
        {
            sql += string.Format(" AND cn.zbhglb LIKE '%{0}%'", jObject["zbhglb"].ToString());
        }

        //处理时间
        if (jObject["checkdate"] != null && jObject["checkdate"].ToString() != "")
        {
            JArray arr = JArray.Parse(jObject["checkdate"].ToString());
            if (arr.Count >= 2)
            {
                sql += string.Format(" AND TRUNC(cn.checkdate) >= to_date('{0}','yyyy-MM-dd')", arr[0].ToString());
                sql += string.Format(" AND TRUNC(cn.checkdate) <= to_date('{0}','yyyy-MM-dd')", arr[1].ToString());
            }
        }

        //机型
        if (jObject["zjx2"] != null && jObject["zjx2"].ToString() != "")
        {
            sql += string.Format(" AND cn.zjx2 LIKE '%{0}%'", jObject["zjx2"].ToString());
        }

        //件号
        if (jObject["matnr"] != null && jObject["matnr"].ToString() != "")
        {
            sql += string.Format(" AND cn.matnr LIKE '%{0}%'", jObject["matnr"].ToString());
        }

        //申请人
        if (jObject["fullname"] != null && jObject["fullname"].ToString() != "")
        {
            sql += string.Format(" AND e.fullname LIKE '%{0}%'", jObject["fullname"].ToString());
        }
        return sql;
    }

    //查询方法
    Result getData(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        uMESPagingDataDTO dto = dal.GetPagingDataIns(sql, (int)jObject["currentPage"], (int)jObject["pageSize"]);
        DataTable dt = dto.DBTable;
        foreach (DataColumn col in dt.Columns)
        {
            col.ColumnName = col.ColumnName.ToLower();
        }
        Result r = new Result();
        r.data = dt;
        r.total = Convert.ToInt32(dto.RowCount);
        r.meta = new Result.Meta(200, "请求成功");
        return r;
    }

    //导出
    void exportExcel(JObject jObject)
    {
        string sql = getQuerySql(jObject);
        DataTable dt = dal.GetDataTable(sql);
        dt.Columns["attributevalue"].Caption = "发动机号";
        dt.Columns["zjx2"].Caption = "机型";
        dt.Columns["matnr"].Caption = "零件图号";
        dt.Columns["description"].Caption = "零件名称";
        dt.Columns["zbhgps"].Caption = "不合格数";
        dt.Columns["checkemployee"].Caption = "申请人";
        dt.Columns["createdate"].Caption = "申请日期";
        dt.Columns["zbhgdh"].Caption = "审理单号";
        dt.Columns["zbhglb"].Caption = "审理类型";
        dt.Columns["zqxtxt"].Caption = "不合格情况";
        dt.Columns["zsljl"].Caption = "审理结果";
        dt.Columns["checkdate"].Caption = "处理日期";
        dt.Columns["zzttxt"].Caption = "备注";
        ExportHelper.ExportExcel(dt, jObject["factoryname"].ToString() + "不合格品审理报表");
    }
}